#include "z80_sio.h"
#include "z80_sd.h"
#include "z80.h"
#include <stdio.h>

// I/O event descriptor
static PollFd Fd;

void Z80_Dispatch()
{
    if (Z80_Poll(&Fd)) {
        // SIO device
        if (Fd.addr >= Z80_SIO_CTRL && Fd.addr <= Z80_SIO_DATA) {
            if (Fd.event == IO_WRITE) {
                SIO_Write(Fd.addr, Fd.wdata);
            } else {
                SIO_Read(Fd.addr, Fd.rdata);
            }
        }
        
        // MicroSD device
        if (Fd.addr >= Z80_SD_DATA && Fd.addr <= Z80_SD_STATUS) {
            if (Fd.event == IO_WRITE) {
                SD_Write(Fd.addr, Fd.wdata);
            } else {
                SD_Read(Fd.addr, Fd.rdata);
            }
        }

        // Operation complete.
        Z80_Finish();
    }
}